﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class MDesktop : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Head1.Title = "欢迎登录" + ConfigurationManager.AppSettings["CompanyName"]
                + ConfigurationManager.AppSettings["SystemName"];
            Mtitle.InnerText = ConfigurationManager.AppSettings["SystemName"].Substring(0, 6);
            MFoot.InnerText = DateTime.Now.Year.ToString() + "©"
                + ConfigurationManager.AppSettings["CompanyName"] + "Gt";
            Menu_DataBind();
        }
    }

    private void Menu_DataBind()
    {
        string sqlstr = @"SELECT DISTINCT T4.MENU_URL AS page_url,T4.MENU_TITLE AS page_title,T4.MENU_NUMBER AS order_num
                                      FROM U_USER_ROLE_T T1 
                                     INNER JOIN (SELECT data_code,data_name FROM U_PUB_DATA_T
                                                  WHERE data_type = 'role_list' AND if_valid = 'Y') T2
                                        ON T1.ROLE_CODE = T2.DATA_CODE
                                     INNER JOIN U_ROLE_PAGE_T T3
                                        ON T1.ROLE_CODE = T3.ROLE_CODE
                                       AND T3.IF_VALID = 'Y'
                                     INNER JOIN U_MENU_T T4
                                        ON T3.PAGE_CODE = T4.MENU_TITLE
                                       AND T4.MENU_STATUS = '是'
                                       AND T4.IF_MOBILE = 'Y'
                                     WHERE T1.Login_Name = :login_name
                                       AND T1.If_Valid = 'Y'
                                     ORDER BY T4.MENU_NUMBER";
        OracleParameter[] _params = {
            new OracleParameter(":login_name",OracleType.VarChar,16)
        };
        _params[0].Value = LoginUser;
        DataSet ds = OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, _params);
        RenderORRedirect(ds.Tables[0]);
        MenuList.DataSource = ds;
        MenuList.DataBind();        
    }

    private void RenderORRedirect(DataTable argDt)
    {
        if (argDt.Rows.Count == 0)
        {
            msg.Text = "用户权限配置有误，请退出系统并联系系统管理员！";
            log4net.LogManager.GetLogger(GetType()).Error("移动端桌面用户" + Session["LoginUser"].ToString()+"未配置权限！");
        }
        else if (argDt.Rows.Count == 1)
        {
            Response.Redirect("~/" + argDt.Rows[0]["page_url"].ToString());            
        }
    }
}
